package org.skydiveapplibrary.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public final class ZipUtil
{
	private static final int BUFFER = 2048;
	
	public static void createZip(
			OutputStream outputStream,
			List<File> files)
		throws IOException
	{
		// create zip output stream
		ZipOutputStream zipOutputStream =
			new ZipOutputStream(new BufferedOutputStream(outputStream));
        
		// add files
		InputStream inputStream;
		int count;
        byte data[] = new byte[BUFFER];
        for (File file: files)
        {
        	// create input stream
        	inputStream = new BufferedInputStream(
        			new FileInputStream(file),
        			BUFFER);
        	
        	// add zip entry
        	zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
        	
        	// copy data
        	while((count = inputStream.read(data, 0, BUFFER)) != -1)
            {
            	zipOutputStream.write(data, 0, count);
            }
        	
        	// close input stream
        	inputStream.close();
        }
        
        // close zip
        zipOutputStream.close();
	}
}
